package com.example.natatorium_management.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.natatorium_management.entity.Gymnasium;
import com.example.natatorium_management.mapper.GymnasiumMapper;
import com.example.natatorium_management.result.restful.R;
import com.example.natatorium_management.result.restful.ResponseEunm;
import com.example.natatorium_management.service.GymnasiumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class GymnasiumServiceImpl extends ServiceImpl<GymnasiumMapper, Gymnasium> implements GymnasiumService {
    @Autowired
    GymnasiumMapper gymnasiumMapper;

    /**
     * 分页游泳馆
     * @param current 1
     * @param size 1
     * @return 1
     */
    @Override
    public R<IPage<Gymnasium>> QueryPage(int current, int size){
        Page<Gymnasium> of = Page.of(current, size);
        return new R<>(ResponseEunm.SUCCESS, gymnasiumMapper.QueryPage(of));
    }

    /**
     * 新增游泳馆
     * @param gymnasium 1
     * @return 1
     */
    @Override
    public R<Gymnasium> addGymnasium(Gymnasium gymnasium){
        if (save(gymnasium)){
            return new R<>(ResponseEunm.SUCCESS,null);
        }
        return new R<>(ResponseEunm.ADD_FAIL,null);
    }

    /**
     * 通过id找游泳馆
     * @param gymnasiumId 1
     * @return 1
     */
    @Override
    public R<Gymnasium> findGymnasiumById(int gymnasiumId){
        return new R<>(ResponseEunm.SUCCESS,gymnasiumMapper.findGymnasiumById(gymnasiumId));
    }

    /**
     * 更新游泳馆
     * @param gymnasium 1
     * @return 1
     */
    public R<Gymnasium> updateGymnasium(Gymnasium gymnasium){
        if(gymnasiumMapper.updateGymnasium(gymnasium)==1) {
            return new R<>(ResponseEunm.SUCCESS, null);
        }
        return new R<>(ResponseEunm.UPDATE_FAIL,null);
    }

    @Override
    public R<IPage<Gymnasium>> findGymnasium(Gymnasium gymnasium,int current,int size) {
        if(gymnasium.getGymnasiumName()==null){
            return new R<>(ResponseEunm.SUCCESS,gymnasiumMapper.findGymnasiumByPhone(Page.of(current,size),gymnasium.getPhone()));
        }
        if(gymnasium.getPhone()==null){
            return new R<>(ResponseEunm.SUCCESS,gymnasiumMapper.findGymnasiumByName(Page.of(current,size),gymnasium.getGymnasiumName()));
        }
        return new R<>(ResponseEunm.SUCCESS,gymnasiumMapper.findGymnasiumByNameAndPhone(Page.of(current,size),gymnasium));
    }

    @Override
    public R<Gymnasium> deleteGymnasiumById(int id) {
        if(gymnasiumMapper.deleteById(id)==1){
            return new R<>(ResponseEunm.SUCCESS,null);
        }
        return new R<>(ResponseEunm.DELETE_FAIL,null);
    }
}
